package com.dream.technology.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dream.technology.data.NoBody;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dream.technology.db.entity.RcFlow;
import com.dream.technology.db.entity.RcRuleCollection;
import com.dream.technology.db.entity.RcRuleCollectionItem;
import com.dream.technology.db.entity.RcRuleCollectionQuery;
import com.dream.technology.db.entity.vo.VoRuleCollection;
import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.service.RcFlowService;
import com.dream.technology.service.RcRuleCollectionItemService;
import com.dream.technology.service.RcRuleCollectionService;
import com.dream.technology.vo.ErrorCode;

@RestController
@RequestMapping("/risk/admin/rule_collection")
public class RcRuleCollectionController {

    private static final Logger logger = LoggerFactory.getLogger(RcRuleCollectionController.class);

    @Autowired
    RcRuleCollectionService rcRuleCollectionService;
    @Autowired
    RcRuleCollectionItemService rcRuleCollectionItemService;
    @Autowired
    private RcFlowService rcFlowService;

    /**
     * 风控规则集接口
     * @param pageNum
     * @param pageSize
     * @param code
     * @param name
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize, String collectionName, Integer classifyId){

        RcRuleCollectionQuery query =new RcRuleCollectionQuery();
        RcRuleCollectionQuery.Criteria criteria1 = query.createCriteria();
        RcRuleCollectionQuery.Criteria criteria2 = query.createCriteria();
        criteria1.andStatusEqualTo(false);
        if (classifyId != null) {
            criteria1.andClassifyIdEqualTo(classifyId);
        }
        if (StringUtils.isNotBlank(collectionName)){
            criteria1.andCollectionNameLike("%" + collectionName + "%");
            criteria2.andCollectionCodeLike("%" + collectionName + "%");
        }
        query.or(criteria2);
        query.setOrderByClause("enabled desc,create_date desc");
        PageHelper.startPage(pageNum,pageSize);
        List<RcRuleCollection> rcRuleCollectionList = rcRuleCollectionService.selectByExample(query);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), new PageInfo<RcRuleCollection>(rcRuleCollectionList));
    }

    /**
     * 风控规则集回显接口
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<VoRuleCollection> getRcRuleCollectionById(Long id){

        RcRuleCollection rcRuleCollection = rcRuleCollectionService.selectByPrimaryKey(id);
        List<RcRuleCollectionItem> rcRuleCollectionItems = rcRuleCollectionItemService.selectByCollectionId(id);
        VoRuleCollection collection = new VoRuleCollection();
        collection.setRcRuleCollection(rcRuleCollection);
        collection.setRcRuleCollectionItem(rcRuleCollectionItems);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), collection);

    }

    /**
     * 添加风控规则集
     * @param rcRuleCollectionStr
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addRcRuleCollection( @RequestBody VoRuleCollection rcRuleCollectionStr){

        rcRuleCollectionService.insertStr(rcRuleCollectionStr);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 修改风控规则集
     * @param jsonStr
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateRcRuleCollection(@RequestBody VoRuleCollection jsonStr){

        rcRuleCollectionService.updateByJsonStr(jsonStr);


        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     *  删除风控规则集 (逻辑删除)
     * @param id
     * @return
     */
    @PostMapping("/deleteOrStop")
    public GenericRspDTO<NoBody> updateRcRuleCollection(Long id, Boolean status, Boolean enabled){
    	RcRuleCollection ruleCollection = new RcRuleCollection();
    	ruleCollection.setId(id);
    	if (status != null) {
    		ruleCollection.setStatus(status);
		}
		if (enabled != null) {
			ruleCollection.setEnabled(enabled);
		}
        rcRuleCollectionService.updateByPrimaryKeySelective(ruleCollection);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }



    /**
     * 风控规则集验证
     * @param
     * @return
     */
    @PostMapping("/checkForDoc")
    public GenericRspDTO<Object> checkRcCustomerTag(@RequestBody VoRuleCollection ruleCollection){


        String result = rcRuleCollectionService.checkRuleCollection(ruleCollection);


        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), result);
    }



    /**
     * 风控规则集检查引用 ，规则流程
     * @param
     * @return
     */
    @PostMapping("/checkRef")
    public GenericRspDTO<Object> updateBeforeCheck(Long id){
    	List<RcFlow> result = rcFlowService.queryFlowByTag(2, id);
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("count", result.size());
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), map);
    }



    @PostMapping("/copy")
    public GenericRspDTO<Object> copy(Long id, String collectionName, String collectionDetail, Boolean enabled){

        String copy = rcRuleCollectionService.copy(id, collectionName, collectionDetail, enabled);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), "");
    }
    
    /**
     * 验证名称是否重复
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public GenericRspDTO<Object> getByName(String name){
    	RcRuleCollectionQuery query = new RcRuleCollectionQuery();
    	RcRuleCollectionQuery.Criteria criteria = query.createCriteria();
    	criteria.andCollectionNameEqualTo(name);
    	int count = rcRuleCollectionService.selectByExample(query).size();
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }

}
